package com.softaria.spkiller.mojo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.maven.plugin.MojoExecutionException;

import com.softaria.spkiller.layers.classificator.impl.IOProblemCollector;
import com.softaria.spkiller.metadata.ClassIdentifier;

public class MojoIoProblemCollector implements IOProblemCollector {

	private class IOProblem {
		
		ClassIdentifier classID;
		IOException ex;
		
		public IOProblem(ClassIdentifier classID, IOException ex) {
			super();
			this.classID = classID;
			this.ex = ex;
		}

		public ClassIdentifier getClassID() {
			return classID;
		}

		public IOException getEx() {
			return ex;
		}
		
	}
	
	private Collection<ClassIdentifier> notFound = new ArrayList<ClassIdentifier>();
	private Collection<IOProblem> ioProblems = new ArrayList<IOProblem>();
	
	public void reportClassNotFound(ClassIdentifier id) {
		notFound.add(id);
	}

	public void reportIoException(ClassIdentifier id, IOException ex) {
		ioProblems.add(new IOProblem(id, ex));
	}

	public void check() throws MojoExecutionException {
		for(ClassIdentifier id:notFound) {
			System.out.println("SPAGHETTI-KILLER ERROR: Class not found "+id);
		}
		
		for(IOProblem problem:ioProblems) {
			System.out.println("SPAGHETTI-KILLER ERROR: I/O problem with class "+problem.getClassID()+" : "+problem.getEx().getMessage());
			problem.getEx().printStackTrace();
		}
		
		if(!notFound.isEmpty()||!ioProblems.isEmpty()) {
			throw new MojoExecutionException("See above");
		}

	}
}
